package com.uzai.console.web.locallive;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.jd.jdorder.JdOrderIdDto;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.locallive.elmorder.*;
import com.uzai.console.dto.tb.tbtoken.TbTokenQuery;
import com.uzai.console.entity.DeviceType;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.TbToken;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.TbTokenMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.device.DeviceTypeService;
import com.uzai.console.service.locallive.ElmOrderService;
import com.uzai.console.service.order.OrderReStatusService;
import com.uzai.console.service.order.OrderSourceService;
import com.uzai.console.service.order.OrderStatusService;
import com.uzai.console.service.tb.tborder.TbCategoryService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.user.UserInfoVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceWechatInfoSelectVo;
import com.uzai.console.vo.locallive.elmorder.ElmOrderInfoVo;
import com.uzai.console.vo.locallive.elmorder.ElmOrderReportVo;
import com.uzai.console.vo.order.OrderReStatusVo;
import com.uzai.console.vo.order.OrderSourceVo;
import com.uzai.console.vo.order.OrderStatusVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 饿了么订单
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "饿了么订单",tags = "本地生活|饿了么订单")
@ApiSort(13)
@RestController
public class EmlOrderController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(EmlOrderController.class);

    @Autowired
    private ElmOrderService elmOrderService;
    @Autowired
    private TbCategoryService tbCategoryService;
    @Autowired
    private OrderStatusService orderStatusService;
    @Autowired
    private OrderReStatusService orderReStatusService;
    @Autowired
    private OrderSourceService orderSourceService;
    @Autowired
    private TbTokenMapper tbTokenMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "查询饿了么订单列表", notes = "查询饿了么订单列表", response = ElmOrderInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/elmOrder/findElmOrderList")
    public Object findElmOrderList(@RequestBody ElmOrderQuery elmOrderQuery){
        return elmOrderService.findElmOrderList(elmOrderQuery);
    }

    @ApiOperation(value = "查询饿了么订单详情", notes = "查询饿了么订单详情", response = ElmOrderInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/elmOrder/findElmOrderInfo")
    public Object findElmOrderInfo(@RequestBody ElmOrderIdDto elmOrderIdDto){
        return elmOrderService.findElmOrderInfo(elmOrderIdDto);
    }

    @ApiOperation(value = "查询饿了么订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", notes = "查询饿了么订单统计信息(总收入、买家总金额、总利润、未绑定用户利润、未收货返利金额）", response = ElmOrderReportVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/elmOrder/findElmOrderReport")
    public Object findElmOrderReport(@RequestBody ElmOrderQuery elmOrderQuery){
        return elmOrderService.findElmOrderRepot(elmOrderQuery);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "订单删除", notes = "订单删除")
    @ApiOperationSupport(order = 4)
    @PostMapping("/elmOrder/delElmOrder")
    public Object delElmOrder(@RequestBody ElmOrderDelDto elmOrderDelDto){
        return elmOrderService.delElmOrder(elmOrderDelDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "确认收货", notes = "确认收货")
    @ApiOperationSupport(order = 5)
    @PostMapping("/elmOrder/confirmReceive")
    public Object confirmReceive(@RequestBody ElmOrderIdDto elmOrderIdDto){
        return elmOrderService.confirmReceive(elmOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "手动维权", notes = "手动维权")
    @ApiOperationSupport(order = 6)
    @PostMapping("/elmOrder/confirmRefund")
    public Object confirmRefund(@RequestBody ElmOrderIdDto elmOrderIdDto){
        return elmOrderService.confirmRefund(elmOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单冻结", notes = "订单冻结")
    @ApiOperationSupport(order = 7)
    @PostMapping("/elmOrder/confirmFrozen")
    public Object confirmFrozen(@RequestBody ElmOrderIdDto elmOrderIdDto){
        return elmOrderService.confirmFrozen(elmOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单解冻", notes = "订单解冻")
    @ApiOperationSupport(order = 8)
    @PostMapping("/elmOrder/confirmThaw")
    public Object confirmThaw(@RequestBody ElmOrderIdDto elmOrderIdDto){
        return elmOrderService.confirmThaw(elmOrderIdDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "解绑用户", notes = "解绑用户")
    @ApiOperationSupport(order = 9)
    @PostMapping("/elmOrder/unbindUser")
    public Object unBindUser(@RequestBody ElmOrderUnbindUserDto elmOrderUnbindUserDto){
        return elmOrderService.unbindUser(elmOrderUnbindUserDto);
    }

    @ApiOperation(value = "绑定用户", notes = "绑定用户")
    @ApiOperationSupport(order = 10)
    @PostMapping("/elmOrder/bindUser")
    public Object bindUser(@RequestBody ElmOrderBindUserDto elmOrderBindUserDto){
        return elmOrderService.bindUser(elmOrderBindUserDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改解冻时间", notes = "修改解冻时间")
    @ApiOperationSupport(order = 11)
    @PostMapping("/elmOrder/updateThawTime")
    public Object updateThawTime(@RequestBody ElmOrderThawTimeDto elmOrderThawTimeDto){
        return elmOrderService.updateThawTime(elmOrderThawTimeDto);
    }

    @ApiOperation(value = "查询饿了么联盟账号接口", notes = "查询饿了么联盟账号接口", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 12)
    @PostMapping("/elmOrder/findAlimmList")
    public Object findAlimmList(){
        //返回给页面的列表
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        //查询对象
        TbTokenQuery tbTokenQuery = new TbTokenQuery();
        tbTokenQuery.setMerId(getLoginInfo().getMerId());
        tbTokenQuery.setType(1);//饿了么
        List<TbToken> tbTokenList = tbTokenMapper.findByList(tbTokenQuery);
        if(tbTokenList != null && tbTokenList.size() > 0){
            int now = DateUtil.getNowTime();
            for (TbToken tbToken : tbTokenList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(tbToken.getId()));
                selectKeyVo.setValue(tbToken.getUserName());
                if(tbToken.getExptime().intValue() <= now){
                    selectKeyVo.setExptimeFlag(1); //已到期
                }else {
                    selectKeyVo.setExptimeFlag(0); //未到期
                }
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "手动下载饿了么订单", notes = "手动下载饿了么订单")
    @ApiOperationSupport(order = 13)
    @PostMapping("/elmOrder/downloadOrder")
    public Object downloadOrder(@RequestBody ElmOrderDownLoadDto elmOrderDownLoadDto){
        elmOrderService.downloadOrder(elmOrderDownLoadDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "订单刷新", notes = "订单刷新")
    @ApiOperationSupport(order = 14)
    @PostMapping("/elmOrder/refreshOrder")
    public Object refreshOrder(@RequestBody ElmOrderIdDto elmOrderIdDto){
        elmOrderService.refreshOrder(elmOrderIdDto);
        return true;
    }

    @ApiOperation(value = "查询所有未绑定用户订单", notes = "查询所有未绑定用户订单", response = ElmOrderInfoVo.class)
    @ApiOperationSupport(order = 15)
    @PostMapping("/elmOrder/findUnbindUserList")
    public Object findUnbindUserList(@RequestBody ElmOrderUnbindDtoQuery elmOrderUnbindDtoQuery){
        return elmOrderService.findUnbindUserList(elmOrderUnbindDtoQuery);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除所有未绑定用户订单", notes = "删除所有未绑定用户订单", response = Boolean.class)
    @ApiOperationSupport(order = 16)
    @PostMapping("/elmOrder/deleteUnbindUserList")
    public Object deleteUnbindUserList(@RequestBody ElmOrderDeleteUnbindDto elmOrderDeleteUnbindDto){
        return elmOrderService.deleteUnbindUserList(elmOrderDeleteUnbindDto);
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPLOAD)
    @ApiOperation(value = "更新7天前未收货订单", notes = "更新7天前未收货订单")
    @ApiOperationSupport(order = 17)
    @PostMapping("/elmOrder/updateElmOrderUnReceived")
    public Object updateElmOrderUnReceived(){
        elmOrderService.updateElmOrderUnReceivedList(getLoginInfo().getMerId());
        return true;
    }

    @ApiOperation(value = "查询饿了么订单状态列表", notes = "查询饿了么订单状态列表", response = OrderStatusVo.class)
    @ApiOperationSupport(order = 19)
    @PostMapping("/elmOrder/getOrderStatusList")
    public Object getOrderStatusList() {
        return orderStatusService.findElmOrderStatusList();
    }

    @ApiOperation(value = "查询饿了么订单返利状态列表", notes = "查询饿了么订单返利状态列表", response = OrderReStatusVo.class)
    @ApiOperationSupport(order = 20)
    @PostMapping("/elmOrder/getOrderReStatusList")
    public Object getOrderReStatusList() {
        return orderReStatusService.findElmOrderReStatusList();
    }

    @ApiOperation(value = "查询饿了么订单来源列表", notes = "查询饿了么订单订单来源列表", response = OrderSourceVo.class)
    @ApiOperationSupport(order = 22)
    @PostMapping("/elmOrder/getOrderSourceList")
    public Object getOrderSourceList() {
        return orderSourceService.findOrderSourceList();
    }

    @ApiOperation(value = "查询所有未绑定用户统计信息(总收入、买家总金额、总利润）", notes = "查询所有未绑定用户订单统计信息(总收入、买家总金额、总利润）", response = ElmOrderReportVo.class)
    @ApiOperationSupport(order = 23)
    @PostMapping("/elmOrder/findUnbindUserReport")
    public Object findUnbindUserReport(@RequestBody ElmOrderUnbindDtoQuery elmOrderUnbindDtoQuery){
        return elmOrderService.findUnbindUserReport(elmOrderUnbindDtoQuery);
    }

    @ApiOperation(value = "查询机器人下拉列表", notes = "查询机器人下拉列表", response = DeviceInfoVo.class)
    @ApiOperationSupport(order = 24)
    @PostMapping("/elmOrder/findDeviceList")
    public Object findDeviceList(){
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setMerId(getLoginInfo().getMerId());
        return deviceService.findDeviceList(deviceQuery);
    }

    @ApiOperation(value = "分页查询会员信息列表", notes = "分页查询会员信息列表", response = UserInfoVo.class)
    @ApiOperationSupport(order = 25)
    @PostMapping("/elmOrder/findUserPage")
    public Object findUserPage(@RequestBody UserQuery userQuery) {
        return userService.findUserPage(userQuery);
    }

    @ApiOperation(value = "查询设备类型下拉列表", notes = "查询设备类型下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 26)
    @PostMapping("/elmOrder/findDeiviceTypeListSelect")
    public Object findDeiviceTypeListSelect() {
        return deviceTypeService.findDeviceTypeListSelect();
    }

    @SystemLogType(model = SystemModelEnum.EmlOrder, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "追回返利", notes = "追回返利")
    @ApiOperationSupport(order = 24)
    @PostMapping("/elmOrder/rebateRefound")
    public Object rebateRefound(@RequestBody ElmOrderIdDto elmOrderIdDto){
        return elmOrderService.rebateRefound(elmOrderIdDto);
    }
}
